﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class UserBLL:IUserBLL
    {
        public IUserDAL<User> Dal { get; }

        public UserBLL(IUserDAL<User> dal)
        {
            Dal = dal;
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">页容量</param>
        /// <param name="name">姓名 模糊</param>
        /// <param name="lx">类型 下拉</param>
        /// <returns>通过分页+条件查询来返回数据</returns>
       public  UserDTO UShow(int PageIndex, int PageSize, string? name, string? lx)
        {
            var list=Dal.GetAllAsync().Where(x=>x.IsDel==true);
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.Username.Contains(name));
            }
            if (!string.IsNullOrEmpty(lx))
            {
                list = list.Where(x => x.UserLx.Equals(lx));
            }
            UserDTO userDTO = new UserDTO();
            userDTO.count=list.Count();
            userDTO.users=list.OrderBy(x=>x.UserId).Skip((PageIndex-1)*PageSize).Take(PageSize).ToList();
            return userDTO;
        }
        /// <summary>
        /// 逻辑批量删除
        /// </summary>
        /// <param name="ids">居民id</param>
        /// <returns>返回匿名函数</returns>
        public async Task<int> Del(string? ids)
        {
            var a = Dal.GetAllAsync();
            List<int> ints=ids.Split(",").Select(x=>Convert.ToInt32(x)).ToList();
            a = a.Where(x=>ints.Contains(x.UserId));
            foreach (var i in a)
            {
                i.IsDel = false;
            }
            return await Dal.UpdateAsyncd(a.ToList());
        }
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids">居民id</param>
        /// <returns>返回匿名函数</returns>
        public async Task<int> Upd(string? ids)
        {
            var a = Dal.GetAllAsync();
            List<int> ints = ids.Split(",").Select(x => Convert.ToInt32(x)).ToList();
            a = a.Where(x => ints.Contains(x.UserId));
            foreach (var i in a)
            {
                i.state = false;
            }
            return await Dal.UpdateAsyncd(a.ToList());
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="ids">居民id</param>
        /// <returns>查询某一条数据</returns>
        public  User Fan(int ids)
        {
            var a = Dal.GetAllAsync().FirstOrDefault(x=>x.UserId==ids);
            return a;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m">居民信息</param>
        /// <returns>修改受影响行数</returns>
        public   async Task<int> Update(User m)
        {
            return await Dal.UpdateAsync(m);
        }
    }
}
